/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import ui.common.AbstractUIBean;

import util.exception.FileOverSize;

/**
 *
 * @author Neo
 */
@ManagedBean
@RequestScoped
public class UploadBean extends AbstractUIBean {

    /**
     * Creates a new instance of UploadBean
     */
    public UploadBean() {
    }
    private UploadedFile uploadedFile;

    public UploadedFile getUploadFile() {
        return uploadedFile;
    }

    public void setUploadFile(UploadedFile uploadedFile1) {
        this.uploadedFile = uploadedFile1;
    }

    public String uploadImage() throws IOException, FileOverSize {
        String fileName = FilenameUtils.getName(uploadedFile.getName());
        String contentType = uploadedFile.getContentType();
        byte[] bytes = uploadedFile.getBytes();
        String rd = RandomGenerator.randomString(30);
        String path = exContext.getRealPath("/").replace("\\", "/").replace("/build/web/", "") + "/web/resources/imageupload/";
        File f = new File(path);
        if (!f.exists()) {
            f.mkdir();
        }
        f.getAbsolutePath();
        path = path + rd + ".jpg";
        f = new File(path);
        FileOutputStream output = new FileOutputStream(new File(path));
        IOUtils.write(bytes, output);
        output.close();
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
        return path;
//        if (bytes.length <= 200000) {
//            return path;
//        } else {
//            throw new FileOverSize();
//
//        }


    }

    public String uploadAttachment() throws IOException {
        String fileName = FilenameUtils.getName(uploadedFile.getName());
        String contentType = uploadedFile.getContentType();
        byte[] bytes = uploadedFile.getBytes();
        String rd = RandomGenerator.randomString(30);
        String path = exContext.getRealPath("/").replace("\\", "/").replace("/build/web/", "") + "/web/resources/attachment/" + rd + ".jpg";
        FileOutputStream output = new FileOutputStream(new File(path));
        IOUtils.write(bytes, output);
        output.close();
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
        return path;
    }

    public void submit() throws IOException {
        String fileName = FilenameUtils.getName(uploadedFile.getName());
        String contentType = uploadedFile.getContentType();
        byte[] bytes = uploadedFile.getBytes();
        String rd = RandomGenerator.randomString(30);
        FileOutputStream output = new FileOutputStream(new File("D:\\abc.jpg"));
        IOUtils.write(bytes, output);
        output.close();
        // Now you can save bytes in DB (and also content type?)

        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
    }

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }
}
